var base = getApp();
const db=wx.cloud.database()
Page({
    data: {
        userInfo:'',
        jzb: "0",
        coupon: '0',
        loaded: true,
        exp:0,
    
        levels: 0,
        headimg: "https://m.bestcake.com/images/icon_user.jpg",
       Me:[]
    },
    // 极致币bindtap
    yuan:function(e)
{
wx.navigateTo({
  url: '../JiZhiBi/JiZhiBi',
})
},
// 优惠券bindtap
zhang:function(e)
{
wx.navigateTo({
  url: '../YouHuiQuan/YouHuiQuan',
})
},
// 经验bindtap
fen:function(e)
{
wx.navigateTo({
  url: '../JingYan/JingYan',
})
},
// 授权登陆
    onGotUserInfo:function (e) {//授权登陆
        const { userInfo } = e.detail
        console.log(e)
        this.setData({
            userInfo: userInfo
        })
    },
    // 功能区
    thistap:function(e){
      console.log(e)
      if("我的订单"===e.currentTarget.dataset.text){
        wx.navigateTo({
          url: '/pages/DingDan/DingDan',
       } )
      }
      if("我的店铺"===e.currentTarget.dataset.text){
        wx.navigateTo({
          url: '/pages/Dianpu/Dianpu',
       } )
      }
      if("收货地址"===e.currentTarget.dataset.text){
        wx.navigateTo({
          url: '/pages/TianJianShuoHuo/TianJianShuoHuo',
       } )
      }
      if("退出登录"===e.currentTarget.dataset.text){
        wx.navigateTo({
          url: '/pages/exit/exit',
       } )
      }
    },    
    onReady: function () {
        // 页面渲染完成
        console.log(base);
    },
//  //更改头像
    changeimg: function () {//更改头像
        var _this = this;
        wx.showModal({
            title: '',
            content: '确定要更换头像？',
            success: function (res) {
                if (res.confirm) {
                    _this.up();
                }
            }
        })
    },
    up: function () {
        var _this = this;
        wx.chooseImage({
            success: function (res) {
                var tempFilePaths = res.tempFilePaths
                wx.uploadFile({
                    url: base.path.www + "upload.ashx", //仅为示例，非真实的接口地址
                    filePath: tempFilePaths[0],
                    name: 'file',
                    formData: {
                        'user': 'test'
                    },
                    success: function (res) {
                        var data = JSON.parse(res.data);
                        data.Tag += '?v=' + Math.random();
                        base.user.headimg = data.Tag;
                        //缓存数据更新
                        var objuser = {};
                        objuser.userid = base.user.userid;
                        objuser.sessionid = base.user.sessionid;
                        objuser.jzb = base.user.jzb;
                        objuser.exp = base.user.exp;
                        objuser.phone = base.user.phone;
                        objuser.levels = base.user.levels;
                        objuser.headimg = data.Tag;
                        base.user.setCache(objuser);
                        _this.setData({
                            headimg: data.Tag
                        });
                        if (data.Status == "ok") {
                            base.get({c: "UserCenter", m: "UpdateMemberHeadImage", imgurl: data.Tag}, function (d) {
                                var d = d.data;
                                if (d.Status == "ok") {
                                }
                            });
                        }
                        else {
                            wx.showModal({
                                showCancel: false,
                                title: '',
                                content: data.Msg
                            })
                        }
                    },
                    faile: function (res) {
                        var data = JSON.parse(res.data);
                        wx.showModal({
                            showCancel: false,
                            title: '',
                            content: data.Msg
                        })
                    }
                })
            }
        })
    },
    onLoad: function () {
      db.collection('Swiper').get({
        success:res=>{
          
          console.log(res.data[0].Me)
          this.setData({Me:res.data[0].Me
            })
        }
       })
        var that = this
        //调用应用实例的方法获取全局数据
        base.getUserInfo(function (userInfo) {
            //更新数据
            that.setData({
                userInfo: userInfo
            })
        })
        if (!base.user.islogin()) {
            wx.redirectTo({
                // url: '../login/login'
                //url: '../phone/phone'
            });
        }
        else {
            this.setData({
                loaded: true,
                jzb: base.user.jzb,
                exp: base.user.exp,
                phone: base.user.phone,
                levels: base.user.levels,
                headimg: base.user.headimg

            });
        }


    }
});